package com.szqhyxj.youxiangju.biz.merchantHome.shop.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.merchantHome.bean.DefaultSite;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * XiaoPanYu
 * on 2018/11/3
 */
public class MySiteAdapter extends BaseAdapter{

    private List<DefaultSite> defaultSites;
    private Context mContext;
    private OnClickListener listener;
    public MySiteAdapter(Context context, List<DefaultSite> defaultSites,OnClickListener listener){
        this.mContext = context;
        this.defaultSites = defaultSites;
        this.listener = listener;
    }

    @Override
    public int getCount(){
        return defaultSites.size();
    }

    @Override
    public Object getItem(int i){
        return defaultSites.get(i);
    }

    @Override
    public long getItemId(int i){
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup){
        if(view == null){
            view = LayoutInflater.from(mContext).inflate(R.layout.list_site_item, null);
            ViewHolder holder = new ViewHolder(view);
            view.setTag(holder);
        }

        DefaultSite site = defaultSites.get(i);
        ViewHolder holder = (ViewHolder) view.getTag();
        holder.userName.setText(site.getUsername());
        holder.userPhone.setText(site.getPhone());
        holder.userSite.setText(site.getFullName()+site.getAddress());
        OnBtnClickListener onBtnClickListener = new OnBtnClickListener(i);
        holder.iconCompile.setOnClickListener(onBtnClickListener);
        holder.compile.setOnClickListener(onBtnClickListener);
        holder.iconRemove.setOnClickListener(onBtnClickListener);
        holder.remove.setOnClickListener(onBtnClickListener);
        holder.iconMoren.setOnClickListener(onBtnClickListener);
        holder.moren.setOnClickListener(onBtnClickListener);

        if(site.isDef()){
            holder.Default.setVisibility(View.VISIBLE);
            holder.iconMoren.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.moren));
        }else{
            holder.Default.setVisibility(View.GONE);
            holder.iconMoren.setImageDrawable(mContext.getResources().getDrawable(R.drawable.background__yuan));
        }
        return view;
    }

     class OnBtnClickListener implements View.OnClickListener{
        private int i;

        public OnBtnClickListener(int i){
            this.i = i;
        }

        @Override
        public void onClick(View view){
            DefaultSite defaultSite = defaultSites.get(i);
            switch(view.getId()){
                case R.id.icon_compile:
                case R.id.compile:
                    listener.editDddress(defaultSite);
                    break;
                case R.id.icon_remove:
                case R.id.remove:
                    listener.deleteDddress(defaultSite.getAddrId()+"");
                    break;
                case R.id.icon_moren:
                case R.id.moren:
                    listener.setDefaults(defaultSite.getAddrId()+"");
                    break;
            }
        }
    }


    public interface OnClickListener{
        void deleteDddress(String addressId);
        void editDddress(DefaultSite site);
        void setDefaults(String addressId);
    }


     static class ViewHolder{
        @BindView(R.id.Default)
        TextView Default;
        @BindView(R.id.user_name)
        TextView userName;
        @BindView(R.id.user_phone)
        TextView userPhone;
        @BindView(R.id.user_site)
        TextView userSite;
        @BindView(R.id.icon_moren)
        ImageView iconMoren;
        @BindView(R.id.icon_compile)
        ImageView iconCompile;
        @BindView(R.id.compile)
        TextView compile;
        @BindView(R.id.icon_remove)
        ImageView iconRemove;
        @BindView(R.id.remove)
        TextView remove;
        @BindView(R.id.moren)
        TextView moren;
        ViewHolder(View view){
            ButterKnife.bind(this, view);
        }
    }
}
